<html>
<head><meta charset="utf-8"><title>expose vec::from_elem · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html">expose vec::from_elem</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225101816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225101816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225101816">(Feb 04 2021 at 00:04)</a>:</h4>
<p>So now that <code>Vec::from([1,2,3])</code> is a stable alternative to <code>vec![1,2,3]</code>, and since <code>Vec::new()</code> as <code>vec![]</code> has always worked, the only invocation of the vec macro without a stable equivalent is <code>vec![1; 2]</code>.</p>
<p>Well... sort of. There does technically exist a public item at <code>std::vec::from_elem</code>, which is what <code>vec![1; 2]</code> expands to. But <code>vec::from_elem</code> is marked <code>#[doc(hidden)]</code> and doesn't exactly give off an air of something that people are supposed to be using. Would people be up for adding <code>Vec::from_elem</code> as a non-secret interface to this?</p>



<a name="225162589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225162589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225162589">(Feb 04 2021 at 14:02)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> A problem with such a function is that the order of the arguments is somewhat arbitrary and can cause confusion. E.g. does <code>from_elem(2, 3)</code> give <code>[2, 2, 2]</code> or <code>[3, 3]</code>? In the macro with the <code>vec![2; 3]</code> syntax this is slightly less of a problem, because it matches the same syntax as <code>[T; N]</code> for arrays.</p>



<a name="225178294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225178294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225178294">(Feb 04 2021 at 15:54)</a>:</h4>
<p>FWIW, that problem also exists for <code>Vec::resize</code>, which has the length first</p>



<a name="225189215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225189215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225189215">(Feb 04 2021 at 16:59)</a>:</h4>
<p>We could mention that it probably isn't a function you want to call directly, and that you should prefer the <code>vec!</code> syntax. But I think we should still document it as existing.</p>



<a name="225199543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225199543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225199543">(Feb 04 2021 at 18:13)</a>:</h4>
<p>Jumping off the deep end a bit, one way to address the ordering thing might be to have the non-macro way be <code>repeat(x).to_vec_len(n)</code> or something.  (That name isn't great, and that form has layering problems, but maybe it'll inspire someone else to come up with a better idea.)</p>
<p>Relatedly, is this a pattern that makes sense for other collections?  It would seem plausible for <code>VecDeque</code>, at least.  So I could also imagine adding <code>fn from_elem(T, n) -&gt; Self { Self::from_iter(repeat(x).take(n)) }</code> to <code>FromIterator</code>, and overriding it for the things that can do better...</p>



<a name="225202146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225202146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225202146">(Feb 04 2021 at 18:32)</a>:</h4>
<p>actually, it occurs to me here that's there's not really any reason for this since <code>Vec::from([1; 2])</code> also works nowadays. you could even change the definition of <code>vec![1; 2]</code> to use that instead of <code>vec::from_elem</code>, though I do see that there's like a billion specialized variants of <code>vec::from_elem</code> so maybe that would be a perf loss...</p>



<a name="225203318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225203318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225203318">(Feb 04 2021 at 18:40)</a>:</h4>
<p>although, wait, <code>Vec::from([1; 2])</code> is more restrictive due to not accepting Clone items. so maybe there is still reason for something else.</p>



<a name="225205541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225205541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225205541">(Feb 04 2021 at 18:58)</a>:</h4>
<p><code>vec![1; n]</code> works when <code>n</code> is not a compile-time constant.</p>



<a name="225207860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225207860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225207860">(Feb 04 2021 at 19:14)</a>:</h4>
<p>Last I looked at this, <code>vec![0; n]</code> was also the only way to get the <code>calloc</code> optimization.</p>



<a name="225219116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225219116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225219116">(Feb 04 2021 at 20:38)</a>:</h4>
<p><span class="user-mention" data-user-id="256342">@bstrie</span> for Copy elements, there's also <code>[123].repeat(3)</code></p>



<a name="225280429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225280429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bluss <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225280429">(Feb 05 2021 at 10:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem/near/225199543">said</a>:</p>
<div class="codehilite"><pre><span></span><code>..
</code></pre></div>
<p>I would be careful to add it to FromIterator - just think about that this is new method may come as an unwanted addition for existing collections - I'm thinking of ndarray :) which implements FromIterator for its one dimensional array and has an existing <code>from_elem(n, T)</code> method (reverse argument order).</p>
<p>In general, adding methods to traits is cute but it does kind of create unfortunate situations downstream. That's something I learned since methods were added to Ord (min/max). It's too easy to add defaulted methods, but should be weighed carefully.</p>



<a name="225424045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225424045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225424045">(Feb 06 2021 at 18:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem/near/225205541">said</a>:</p>
<blockquote>
<p><code>vec![1; n]</code> works when <code>n</code> is not a compile-time constant.</p>
</blockquote>
<p>I'm not sure what this is in response to; all of <code>vec![1; n]</code> and <code>vec::from_elem(1, n)</code>and <code>Vec::from([1; n])</code> compile on stable regardless of if <code>n</code> is a compile-time constant or not</p>



<a name="225424140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225424140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225424140">(Feb 06 2021 at 18:25)</a>:</h4>
<p>that last one definitely does not work if n is not a constant.</p>



<a name="225424446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/expose%20vec%3A%3Afrom_elem/near/225424446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/expose.20vec.3A.3Afrom_elem.html#225424446">(Feb 06 2021 at 18:33)</a>:</h4>
<p><span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span> in my test code I wrote <code>Vec::from([1, n])</code>...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>